﻿using ConsoleApplication1;
using Microsoft.EntityFrameworkCore;
using Prism.Commands;
using Prism.Mvvm;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Text.Json;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using WpfApp2.model;
using zoo.Model.binding;

namespace WpfApp4
{
    internal class Window1ViewModel : BindableBase
    {
        private string _source;

        public string source
        {
            get { return _source; }
            set
            {
                _source = value;
                RaisePropertyChanged(nameof(source));
            }
        }

        private List<animal> _listsource;

        public List<animal> listsource
        {
            get { return _listsource; }
            set
            {
                _listsource = value;
                RaisePropertyChanged(nameof(listsource));
            }
        }

        public Window1ViewModel()
        {
            this.source = "animal";



            //animal animal = new animal();
            //animal.Name = "zhangsan";

            //animal animal1 = new animal();
            //animal.Name = "lisi";

            //this.listsource = new List<animal>();
            //this.listsource.Add(animal);
            //this.listsource.Add(animal1);

            this.listsource = new List<animal>
            {
                new animal { Name = "Lion", Age = 5 },
                new animal { Name = "Tiger", Age = 4 }
            };

            nodes = new ObservableCollection<TreeNode>();

            loadtree();

        }

        public ICommand initdbCommand
        {
            get
            {
                return new DelegateCommand(initdb);
            }
        }
        public ICommand cleardbCommand
        {
            get
            {
                return new DelegateCommand(clear);
            }
        }

        private void clear()
        {
            cleartree();
            loadtree();
        }
        public ObservableCollection<TreeNode> nodes { get; set; }

        public ICommand refreshdbCommand
        {
            get
            {
                return new DelegateCommand(refresh);
            }
        }

        private void refresh()
        {
            loadtree();
        }

        private void loadtree()
        {
            nodes.Clear();
            using (var mydb = new QcsConfigDbContext())
            {
                var query = from a in mydb.pcs.Include("racks").Include("racks.chassiss").Include("racks.chassiss.slots") select a;
                foreach (var item in query)
                {
                    TreeNode Nodepc = new TreeNode { Name = item.Name };
                    Nodepc.NodeType = NodeType.pc;
                    Nodepc.IsExpanded = true;
                    Nodepc.Children = new ObservableCollection<TreeNode>();
                    foreach (var rack in item.racks)
                    {
                        TreeNode Noderack = new TreeNode { Name = rack.Name };
                        Noderack.NodeType = NodeType.rack;

                        Noderack.IsExpanded = true;

                        Noderack.Children = new ObservableCollection<TreeNode>();

                        Nodepc.Children.Add(Noderack);
                        foreach (var chassis in rack.chassiss)
                        {
                            TreeNode Nodechassis = new TreeNode { Name = chassis.Name };
                            Nodechassis.NodeType = NodeType.chassis;

                            Nodechassis.Children = new ObservableCollection<TreeNode>();

                            Noderack.Children.Add(Nodechassis);
                            foreach (var slot in chassis.slots)
                            {
                                TreeNode Nodeslot = new TreeNode { Name = slot.Name };
                                Nodeslot.NodeType = NodeType.slot;

                                Nodechassis.Children.Add(Nodeslot);
                            }
                        }
                    }
                    nodes.Add(Nodepc);
                }
                //TreeNodes = nodes;
            }
        }

        public ObservableCollection<TreeNode> TreeNodes { get; set; }

        private void initdb()
        {
            InitTree();
            loadtree();
            //MessageBox.Show("initdb ok");
        }

        public ICommand OpenDbCommand
        {
            get
            {
                return new DelegateCommand(OpenDbFolder);
            }
        }

        public void OpenDbFolder()
        {
            string dbFolder = Directory.GetCurrentDirectory();

            if (Directory.Exists(dbFolder))
            {
                Process.Start(new ProcessStartInfo()
                {
                    FileName = dbFolder,
                    UseShellExecute = true,
                    Verb = "open"
                });
            }
            else
            {
                // 处理文件夹不存在的情况
                Console.WriteLine("The specified folder does not exist.");
            }
        }

        public void cleartree() {

            using (var mydb = new QcsConfigDbContext())
            {
                try
                {
                    mydb.pcs.RemoveRange(mydb.pcs);
                    mydb.SaveChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }

            }

        }
        public void InitTree()
        {
            cleartree();
            //Student student = new Student { Id = 1, Name = "jack" };
            //Student student2 = new Student { Id = 2, Name = "Tom" };
            //List<Student> list = new List<Student>() { student, student2 };
            //using (var mydb = new QcsConfigDbContext())
            //{
            //    mydb.Students.AddRange(list);
            //    mydb.SaveChanges();
            //}


            pc pc = new pc() { Name = "hp host" };

            List<rack> racks = new List<rack>();
            for (int i = 0; i < 10; i++)
            {
                rack rack = new rack() { Name = "rack" + i, pcId = 1 };
                racks.Add(rack);

                List<chassis> chassiss = new List<chassis>();

                for (int j = 0; j < 6; j++)
                {
                    chassis chassis = new chassis() { Name = "chassis" + i * 10 + j };
                    chassiss.Add(chassis);

                    List<slot> slots = new List<slot>();
                    for (int k = 0; k < 10; k++)
                    {
                        slot slot = new slot() { Name = "slot" + i * 100 + j * 10 + k };
                        slots.Add(slot);
                    }
                    chassis.slots = slots;
                }
                rack.chassiss = chassiss;
            }

            pc.racks = racks;


            using (var mydb = new QcsConfigDbContext())
            {
                try
                {
                    mydb.pcs.Add(pc);
                    mydb.SaveChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                
            }

        }
        public void Initstudent()
        {
            Student student = new Student { Idmy = 1, Name = "jack" };
            Student student2 = new Student { Idmy = 2, Name = "Tom" };
            List<Student> list = new List<Student>() { student, student2 };

            try {

                using (var mydb = new SchoolDbContext())
                {
                    //mydb.Database.EnsureCreated();


                    mydb.Students.AddRange(list);
                    mydb.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
     

            //pc pc = new pc() { Name = "hp host" };

            //List<rack> racks = new List<rack>();
            //for (int i = 0; i < 10; i++)
            //{
            //    rack rack = new rack() { Name = "rack" + i, pcId = 1 };
            //    racks.Add(rack);

            //    List<chassis> chassiss = new List<chassis>();

            //    for (int j = 0; j < 6; j++)
            //    {
            //        chassis chassis = new chassis() { Name = "chassis" + i * 10 + j };
            //        chassiss.Add(chassis);

            //        List<slot> slots = new List<slot>();
            //        for (int k = 0; k < 10; k++)
            //        {
            //            slot slot = new slot() { Name = "slot" + i * 100 + j * 10 + k };
            //            slots.Add(slot);
            //        }
            //        chassis.slots = slots;
            //    }
            //    rack.chassiss = chassiss;
            //}

            //pc.racks = racks;

            //using (var mydb = new QcsConfigDbContext())
            //{
            //    mydb.pcs.Add(pc);
            //    mydb.SaveChanges();
            //}
        }

        public ICommand daochuCommand
        {
            get
            {
                return new DelegateCommand(daochu);
            }
        }

        private void daochu()
        {
            string JsonStr = JsonSerializer.Serialize(listsource);
            File.WriteAllText("animal.json", JsonStr);
            MessageBox.Show("daochu dao animal.json ok");
        }

        public ICommand clearCommand
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    this.listsource = new List<animal>();
                });
            }
        }

        public ICommand openCommand
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    string filePath = "animal.json";
                    if (File.Exists(filePath))
                    {
                        ProcessStartInfo psi = new ProcessStartInfo
                        {
                            FileName = filePath,
                            UseShellExecute = true
                        };
                        Process.Start(psi);
                    }
                    else
                    {
                        MessageBox.Show("File not found.");
                    }
                });
            }
        }

        public ICommand daoruCommand
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    string jsonFilePath = "animal.json";
                    if (File.Exists(jsonFilePath))
                    {
                        string jsonData = File.ReadAllText(jsonFilePath);
                        listsource = JsonSerializer.Deserialize<List<animal>>(jsonData);
                    }
                    else
                    {
                        MessageBox.Show("File not found.");
                    }
                });
            }
        }

        public object JsonConvert { get; private set; }
    }
}